package com.beiye.drivertransport.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.android.frame.adapter.CommonAdapter;
import com.android.frame.adapter.ViewHolder;
import com.beiye.drivertransport.R;
import com.beiye.drivertransport.bean.CourseDetailSubjectBean;
import com.beiye.drivertransport.config.AppInterfaceConfig;
import com.beiye.drivertransport.utils.LogUtils;
import com.beiye.drivertransport.utils.TiShiDialog;
import com.beiye.drivertransport.view.LiumRadioGroup;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by ky on 2018/1/13.
 */

public class CourseDetailRvSubjectApt extends RecyclerView.Adapter<CourseDetailRvSubjectApt.ViewHolder> {
    private final List<CourseDetailSubjectBean.RowsBean> mList;
    private Context context;
    //  private HashMap<String,Boolean> states=new HashMap<String,Boolean>();//记录所有radiobutton被点击的状态

    private LiumRadioGroup radioGroup;

    private RadioButton radioButtonA,radioButtonB,radioButtonC,radioButtonD,radioButtonE,radioButtonF;
    private CheckBox checkBoxA,checkBoxB,checkBoxC,checkBoxD,checkBoxE,checkBoxF;
    private LinearLayout checkbox_ll;

    public CourseDetailRvSubjectApt(Context context, List<CourseDetailSubjectBean.RowsBean> mList) {
        this.context = context;
        this.mList = mList;
    }



    private void buttonShow(Button[] buttons, String[] options, String[] labels) {
        for(int i=0;i<buttons.length;i++){
            if (options[i] == null || options[i].equals("")) {
                buttons[i].setVisibility(View.GONE);
            } else {
                buttons[i].setVisibility(View.VISIBLE);
                buttons[i].setText(labels[i] +". "+ options[i]);
            }
        }
    }

    private void showToastDialog(String str){
        TiShiDialog.Builder builder1 = new TiShiDialog.Builder(context);
        builder1.setMessage(str+"不是正确答案");
        builder1.setTitle("提示:");
        builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder1.create().show();
    }

    private String listToStr(List<String> lists){
        String str="";
        Collections.sort(lists);
        for(int i=0;i<lists.size();i++){
            str+=lists.get(i);
        }
        return str;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view= LayoutInflater.from(context).inflate(R.layout.course_detail_item,viewGroup,false);
        return new CourseDetailRvSubjectApt.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        CourseDetailSubjectBean.RowsBean item = mList.get(i);
        viewHolder.tv_course_title.setText(i + 1 + ". " + mList.get(i).getQuestionDesc());
        if (mList.get(i).getUrl() == null) {
            viewHolder.img_course_img.setVisibility(View.GONE);

        } else {
            viewHolder.img_course_img.setVisibility(View.VISIBLE);

            if (mList.get(i).getUrl().contains("aliyuncs.com")) {
                Picasso.with(context).load(Uri.parse(mList.get(i).getUrl())).placeholder(R.mipmap.no_data2).into( viewHolder.img_course_img);
            } else {

                Picasso.with(context).load(Uri.parse(AppInterfaceConfig.getRequestUrl(mList.get(i).getUrl()))).placeholder(R.mipmap.no_data2).into( viewHolder.img_course_img);
            }
        }

        LogUtils.e("测试", i+"onBindViewHolder: "+item.isSelect());
        LogUtils.e("测试", i+"onBindViewHolder: "+item.isLOOP());



        CheckBox[] checkBoxs=new CheckBox[]{checkBoxA,checkBoxB,checkBoxC,checkBoxD,checkBoxE,checkBoxF};
        RadioButton[] radioButtons=new RadioButton[]{radioButtonA,radioButtonB,radioButtonC,radioButtonD,radioButtonE,radioButtonF};
        String[] options=new String[]{mList.get(i).getOptionA(),mList.get(i).getOptionB(),mList.get(i).getOptionC(),mList.get(i).getOptionD(),mList.get(i).getOptionE(),mList.get(i).getOptionF()};
        String[] numLables=new String[]{"A","B","C","D","E","F"};

        if(mList.get(i).getqType()==2){
            checkbox_ll.setVisibility(View.VISIBLE);
            radioGroup.setVisibility(View.GONE);
            buttonShow(checkBoxs,options,numLables);
        }else{
            checkbox_ll.setVisibility(View.GONE);
            radioGroup.setVisibility(View.VISIBLE);
            buttonShow(radioButtons,options,numLables);
        }

        List<String> answers=new ArrayList<>();
        for(int j=0;j<checkBoxs.length;j++){
            int finalJ = j;
            checkBoxs[j].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!checkBoxs[finalJ].isChecked()){
                        answers.remove(numLables[finalJ]);
                        if(answers.size()<=0){
                            item.setSelect(false);//是否选择
                        }
                    }else{
                        item.setSelect(true);
                        answers.add(numLables[finalJ]);
                        if(!mList.get(i).getRightOption().contains(numLables[finalJ])){
                            showToastDialog(numLables[finalJ]);
                            checkBoxs[finalJ].setChecked(false);
                            answers.remove(numLables[finalJ]);
                        }
                    }
                    LogUtils.e("测试", "CourseDetailRvSubjectApt: "+listToStr(answers) );
                    //判断答案是否正确
                    if(mList.get(i).getRightOption().equals(listToStr(answers))){
                        item.setLOOP(true);
                    }else{
                        item.setLOOP(false);
                    }
                }
            });
        }

        radioGroup.setOnCheckedChangeListener(new LiumRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(LiumRadioGroup group, int checkedId) {
                item.setSelect(true);
                if (checkedId == R.id.radioButton_course) {
                    String right_A = mList.get(i).getRightOption();
                    if ("A".equals(right_A)) {
                        item.setLOOP(true);
                    } else {
                        item.setLOOP(false);
                        showToastDialog("A");
                    }
                } else if (checkedId == R.id.radioButton_course1) {
                    String right_B = mList.get(i).getRightOption();
                    if ("B".equals(right_B)) {
                        item.setLOOP(true);
                    } else {
                        item.setLOOP(false);
                        showToastDialog("B");
                    }
                } else if (checkedId == R.id.radioButton_course2) {
                    String right_C = mList.get(i).getRightOption();
                    if ("C".equals(right_C)) {
                        item.setLOOP(true);
                    } else {
                        item.setLOOP(false);
                        showToastDialog("C");
                    }
                } else if (checkedId == R.id.radioButton_course3) {
                    String right_D = mList.get(i).getRightOption();
                    if ("D".equals(right_D)) {
                        item.setLOOP(true);
                    } else {
                        item.setLOOP(false);
                        showToastDialog("D");
                    }
                } else if (checkedId == R.id.radioButton_course4) {
                    String right_E = mList.get(i).getRightOption();
                    if ("E".equals(right_E)) {
                        item.setLOOP(true);
                    } else {
                        item.setLOOP(false);
                        showToastDialog("E");
                    }
                } else if (checkedId == R.id.radioButton_course5) {
                    String right_F = mList.get(i).getRightOption();
                    if ("F".equals(right_F)) {
                        item.setLOOP(true);
                    } else {
                        item.setLOOP(false);
                        showToastDialog("F");
                    }
                }


            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv_course_title;
        ImageView img_course_img;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);

             tv_course_title = itemView.findViewById(R.id.tv_course2);
             img_course_img = itemView.findViewById(R.id.img_course_item);
            checkbox_ll = (LinearLayout) itemView.findViewById(R.id.checkbox_ll);
            checkBoxA=(CheckBox)itemView.findViewById(R.id.checkbox_course0);
            checkBoxB=(CheckBox)itemView.findViewById(R.id.checkbox_course1);
            checkBoxC=(CheckBox)itemView.findViewById(R.id.checkbox_course2);
            checkBoxD=(CheckBox)itemView.findViewById(R.id.checkbox_course3);
            checkBoxE=(CheckBox)itemView.findViewById(R.id.checkbox_course4);
            checkBoxF=(CheckBox)itemView.findViewById(R.id.checkbox_course5);
            radioGroup = (LiumRadioGroup) itemView.findViewById(R.id.course_rg);
            radioButtonA = (RadioButton) itemView.findViewById(R.id.radioButton_course);
            radioButtonB = (RadioButton) itemView.findViewById(R.id.radioButton_course1);
            radioButtonC = (RadioButton) itemView.findViewById(R.id.radioButton_course2);
            radioButtonD = (RadioButton) itemView.findViewById(R.id.radioButton_course3);
            radioButtonE = (RadioButton) itemView.findViewById(R.id.radioButton_course4);
            radioButtonF = (RadioButton) itemView.findViewById(R.id.radioButton_course5);
        }
    }
}